package gui.dialog;

import java.rmi.RemoteException;

import gui.TuristAssistant;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import org.example.www.webBlogService.Post;

/**
 * Interfaz para el ingreso de un nuevo {@link Comment} en un 
 * {@link Post} especifico.
 * 
 * @author Gonzalo Maso Talou
 *
 */
public class AddCommentDialog extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private StyledText styledText;
	private Button buttonOk;
	private Button buttonCancel;
	/**
	 * Post al cual se esta comentando.
	 */
	private Post post;

	public AddCommentDialog(Shell parent, int style, Post post) {
		super(parent, style);
		this.post = post;
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.NONE);
			dialogShell.setText("Do comment");
			GridLayout dialogShellLayout = new GridLayout();
			dialogShellLayout.numColumns = 2;
			dialogShellLayout.makeColumnsEqualWidth = true;
			dialogShell.setLayout(dialogShellLayout);
			{
				GridData styledTextLData = new GridData();
				styledTextLData.horizontalSpan = 2;
				styledTextLData.grabExcessVerticalSpace = true;
				styledTextLData.grabExcessHorizontalSpace = true;
				styledTextLData.horizontalAlignment = GridData.FILL;
				styledTextLData.verticalAlignment = GridData.FILL;
				styledText = new StyledText(dialogShell, SWT.BORDER);
				styledText.setLayoutData(styledTextLData);
				{
					buttonOk = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
					GridData buttonOkLData = new GridData();
					buttonOkLData.horizontalAlignment = GridData.FILL;
					buttonOkLData.grabExcessHorizontalSpace = true;
					buttonOk.setLayoutData(buttonOkLData);
					buttonOk.setText("Ok");
					buttonOk.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseUp(MouseEvent evt) {
							buttonOkMouseUp(evt);
						}
					});
				}
				{
					buttonCancel = new Button(dialogShell, SWT.PUSH
							| SWT.CENTER);
					GridData buttonCancelLData = new GridData();
					buttonCancelLData.horizontalAlignment = GridData.FILL;
					buttonCancelLData.grabExcessHorizontalSpace = true;
					buttonCancel.setLayoutData(buttonCancelLData);
					buttonCancel.setText("Cancel");
					buttonCancel.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseUp(MouseEvent evt) {
							buttonCancelMouseUp(evt);
						}
					});
				}
			}
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(450, 150);
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void buttonCancelMouseUp(MouseEvent evt) {
		this.dialogShell.close();
	}

	private void buttonOkMouseUp(MouseEvent evt) {
		try {
			TuristAssistant.blogClient.doComment(post.getIdBlog(), post.getIdPost(), TuristAssistant.appContextProperties.getProperty("userID"), TuristAssistant.appContextProperties.getProperty("avatar"), styledText.getText());
			TuristAssistant.blogUpdater.setBlogChanged(post.getIdBlog());
			this.dialogShell.close();		
		} catch (RemoteException e) {
			MessageBox dialog = new MessageBox(this.dialogShell,this.getStyle()|SWT.ERROR|SWT.OK);
			dialog.setMessage("The server do not response. Be sure that a server's instance is running.");
			dialog.setText("Communication server error");
			dialog.open();
		}
	}
}